import 'dart:ui';
import 'package:flutter/material.dart';

class ButtonsDialog extends StatelessWidget {
  const ButtonsDialog({
    Key? key,
    required this.children,
    this.onTaps,
    this.headerView,
  })  : assert(
          !(children == null || children.length == 0),
          '\'children\' cannot be set null or not set.',
        ),
        super(key: key);

  final List<Widget> children;
  final List<Function>? onTaps;
  final Widget? headerView;

  static Future<int?> show(
    BuildContext context, {
    required List<Widget> children,
    List<Function>? onTaps,
    Widget? headerView,
  }) async {
    return await showModalBottomSheet(
          context: context,
          isScrollControlled: true,
          backgroundColor: Colors.transparent,
          elevation: 0,
          builder: (_) => SafeArea(
            child: SizedBox(
              height: 58.0 * (children.length + 1) + 16 + (headerView == null ? 0 : 300),
              child: ButtonsDialog(
                children: children,
                onTaps: onTaps,
                headerView: headerView,
              ),
            ),
          ),
        ).then((value) => value);
  }

  static Future<int?> showList(
    BuildContext context, {
    required List<String> children,
    List<Function>? onTaps,
    Widget? headerView,
  }) async {
    return await showModalBottomSheet(
          context: context,
          isScrollControlled: true,
          backgroundColor: Colors.transparent,
          elevation: 0,
          builder: (_) => SafeArea(
            child: SizedBox(
              height: 58.0 * (children.length + 1) + 16 + (headerView == null ? 0 : 300),
              child: ButtonsDialog(
                children: children
                    .map((e) => Container(
                  alignment: Alignment.center,
                          child: Text(
                            e,
                            style: TextStyle(color: Colors.black, fontSize: 18.0),
                          ),
                        ))
                    .toList(),
                onTaps: onTaps,
                headerView: headerView,
              ),
            ),
          ),
        ) .then((value) => value);
  }

  Widget cancelButton(BuildContext context) {
    return Container(
      height: 57,
      width: double.infinity,
      margin: EdgeInsets.symmetric(horizontal: 10),
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(13))),
      child: InkWell(
        onTap: () {
          Navigator.of(context).pop();
        },
        child: Container(
          alignment: Alignment.center,
          child: Text(
            "取消",
            style: TextStyle(fontSize: 20, color: Colors.grey, fontWeight: FontWeight.w400),
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: WillPopScope(
        onWillPop: () async {
          Navigator.of(context).pop();
          return false;
        },
        child: Align(
          alignment: Alignment.bottomCenter,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              headerView ?? Container(),
              ClipRRect(
                borderRadius: BorderRadius.all(Radius.circular(13)),
                child: BackdropFilter(
                  filter: ImageFilter.blur(
                    sigmaX: 10,
                    sigmaY: 10,
                  ),
                  child: Container(
                    height: 58.0 * children.length,
                    margin: EdgeInsets.symmetric(horizontal: 10),
                    decoration: BoxDecoration(color: Color(0xFFF8F8F8).withOpacity(0.82), borderRadius: BorderRadius.all(Radius.circular(13))),
                    child: ListView.separated(
                      itemCount: children.length,
                      separatorBuilder: (context, index) {
                        return Container(
                          height: 1.0,
                          color: Colors.grey.withOpacity(0.3),
                        );
                      },
                      itemBuilder: (context, index) {
                        return Container(
                          height: 57,
                          child: InkWell(
                            onTap: () {
                              Navigator.of(context).pop(index);
                              if (onTaps != null) {
                                onTaps![index].call();
                              }
                            },
                            child: children[index],
                          ),
                        );
                      },
                    ),
                  ),
                ),
              ),
              SizedBox(height: 8.0),
              cancelButton(context),
              SizedBox(height: 8.0),
            ],
          ),
        ),
      ),
    );
  }
}
